﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
namespace yeqw.FrameWork
{
    public class CaptureInfoHelper
    {
        public static bool UrlExists(string url,string encoding)
        {
            string retString = string.Empty;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
         
            try
            {
                request.UserAgent = "Mozilla/4.0   (compatible;   MSIE   6.0;   Windows   NT   5.1;   SV1;   .NET   CLR  2.0.50727) ";
                request.ServicePoint.Expect100Continue = false;
                request.Method = "GET";
                request.KeepAlive = false;
                request.Accept = "image/gif,   image/x-xbitmap,   image/jpeg,   image/pjpeg,   application/x-shockwave-flash,   application/vnd.ms-excel,   application/vnd.ms-powerpoint,   application/msword,   */*";
                request.ContentType = "application/x-www-form-urlencoded";

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding(encoding));
                retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                if (response.StatusCode == HttpStatusCode.OK)
                    return true;
            }
            catch (Exception ex)
            {
               return false;
            }
            return false;
        }

        #region 同步通过POST方式发送数据

            /// <summary>
        /// 通过POST方式发送数据
        /// </summary>
        /// <param name="Url">url</param>
        /// <param name="postDataStr">Post数据</param>
        /// <param name="cookie">Cookie容器</param>
        /// <returns></returns>
        public static string SendData(string url, string postDataStr, string captureDataMethod, string urlReferer, string encoding)
        {
            string resultmethod = string.Empty;
            return SendData(url,postDataStr,captureDataMethod,urlReferer,encoding,ref resultmethod);
        }
        /// <summary>
        /// 通过POST方式发送数据
        /// </summary>
        /// <param name="Url">url</param>
        /// <param name="postDataStr">Post数据</param>
        /// <param name="cookie">Cookie容器</param>
        /// <returns></returns>
        public static string SendData(string url, string postDataStr, string captureDataMethod, string urlReferer, string encoding, ref string resultcaptureDataMethod)
        {
            string retString = string.Empty;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            //if (cookie.Count == 0)
            //{ref CookieContainer cookie
            //    request.CookieContainer = new CookieContainer();
            //    cookie = request.CookieContainer;
            //}
            //else
            //{
            //    request.CookieContainer = cookie;
            //}
            //起点 gb2312编码
            //逐浪，17看 utf-8编码
            try
            {
                request.UserAgent = "Mozilla/4.0   (compatible;   MSIE   6.0;   Windows   NT   5.1;   SV1;   .NET   CLR  2.0.50727) ";
                request.ServicePoint.Expect100Continue = false;
                request.Method = captureDataMethod;
                request.Referer = urlReferer;
                request.KeepAlive = false;
                request.Accept = "image/gif,   image/x-xbitmap,   image/jpeg,   image/pjpeg,   application/x-shockwave-flash,   application/vnd.ms-excel,   application/vnd.ms-powerpoint,   application/msword,   */*";
                request.ContentType = "application/x-www-form-urlencoded";
                if (captureDataMethod.ToUpper() == "POST")
                {
                    Stream myRequestStream = request.GetRequestStream();
                    StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding(encoding));
                    myStreamWriter.Write(postDataStr);
                    myStreamWriter.Close();
                }
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding(encoding));
                retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
            }
            catch (Exception ex)
            {
                if (ex.Message.IndexOf("405") > -1)
                {   
                    if(resultcaptureDataMethod=="GET")return ex.Message;
                    resultcaptureDataMethod="GET";
                    System.Threading.Thread.Sleep(1000);
                    retString = SendData(url, postDataStr, resultcaptureDataMethod, urlReferer, encoding, ref resultcaptureDataMethod);
                }
            }
            return retString;
        }
       /// <summary>
       /// 获得数据流
       /// </summary>
       /// <param name="url"></param>
       /// <param name="urlReferer"></param>
       /// <returns></returns>
        public static Stream SendDataGetStream(string url, string urlReferer)
        {
            string retString = string.Empty;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            try
            {
                request.UserAgent = "Mozilla/4.0   (compatible;   MSIE   6.0;   Windows   NT   5.1;   SV1;   .NET   CLR  2.0.50727) ";
                request.ServicePoint.Expect100Continue = true;
                request.Method = "GET";
                request.Referer = urlReferer;
                request.KeepAlive = false;
                request.Accept = "image/gif,   image/x-xbitmap,   image/jpeg,   image/pjpeg,   application/x-shockwave-flash,   application/vnd.ms-excel,   application/vnd.ms-powerpoint,   application/msword,   */*";
                request.ContentType = "application/x-www-form-urlencoded";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                return response.GetResponseStream();
                
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        #endregion


        #region 同步通过GET方式发送数据
        /// <summary>
        /// 通过GET方式发送数据
        /// </summary>
        /// <param name="Url">url</param>
        /// <param name="postDataStr">GET数据</param>
        /// <param name="cookie">GET容器</param>
        /// <returns></returns>
        public string SendDataByGET(string Url, string postDataStr, ref CookieContainer cookie)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
            if (cookie.Count == 0)
            {
                request.CookieContainer = new CookieContainer();
                cookie = request.CookieContainer;
            }
            else
            {
                request.CookieContainer = cookie;
            }

            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }
        #endregion
    }
}
